package com.acme.bankapp.service.bank;

import com.acme.bankapp.domain.bank.AbstractAccount;
import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.CheckingAccount;
import com.acme.bankapp.domain.bank.Client;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by anton on 8/26/14.
 */
public class BankReport {
    public static int getNbOfCustomers(Bank bank) {
        return bank.getNbOfClients();
    }

    public static int getNbOfAccounts(Bank bank) {
        int count = 0;
        for (Client c : bank.getClients()) {
            count += c.getNbOfAccounts();
        }
        return count;
    }

    public static List<AbstractAccount> getAccountsSortedBySum(Bank bank) {
        List<AbstractAccount> accounts = new ArrayList<>();
        for(Client c : bank.getClients()) {
            accounts.addAll(c.getAccounts().stream().collect(Collectors.toList()));
        }
        Collections.sort(accounts);
        return accounts;
    }

    public static double getBankCredit(Bank bank) {
        double bankCreadit = 0.0;
        for (Client c : bank.getClients()) {
            for (AbstractAccount a : c.getAccounts()) {
                if (a instanceof CheckingAccount) {
                    CheckingAccount ca = (CheckingAccount) a;
                    if (ca.getBalance() < 0.0) {
                        bankCreadit += Math.abs(ca.getBalance());
                    }
                }
            }
        }
        return bankCreadit;
    }
}
